package com.ydjk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydjk.common.utils.DateUtils;
import com.ydjk.common.utils.StringUtils;
import com.ydjk.common.utils.UuidUtil;
import com.ydjk.common.utils.pageData.PageData;
import com.ydjk.domain.JkInterfaceMessage;
import com.ydjk.domain.JkPushTemplate;
import com.ydjk.domain.JkPushTemplateItem;
import com.ydjk.mapper.JkPushTemplateMapper;
import com.ydjk.service.IJkInterfaceMessageService;
import com.ydjk.service.IJkPushService;
import com.ydjk.service.IJkPushTemplateService;
import com.ydjk.service.JkMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 推送模版Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-29
 */
@Service
public class JkPushTemplateServiceImpl extends ServiceImpl<JkPushTemplateMapper, JkPushTemplateItem> implements IJkPushTemplateService {
    @Autowired
    private JkPushTemplateMapper jkPushTemplateMapper;
    @Autowired
    private JkMessageService jkMessageService;
    @Autowired
    private IJkInterfaceMessageService iJkInterfaceMessageService;

    @Autowired
    private IJkPushService iJkPushService;

    /**
     * 查询推送模版
     *
     * @param id 推送模版主键
     * @return 推送模版
     */
    @Override
    public JkPushTemplate selectJkPushTemplateById(String id) {
        return jkPushTemplateMapper.selectJkPushTemplateById(id);
    }

    /**
     * 查询推送模版列表
     *
     * @param jkPushTemplate 推送模版
     * @return 推送模版
     */
    @Override
    public List<JkPushTemplate> selectJkPushTemplateList(JkPushTemplate jkPushTemplate) {
        return jkPushTemplateMapper.selectJkPushTemplateList(jkPushTemplate);
    }

    /**
     * 新增推送模版
     *
     * @param jkPushTemplate 推送模版
     * @return 结果
     */
    @Override
    public int insertJkPushTemplate(JkPushTemplate jkPushTemplate) {

        String id = UuidUtil.get32UUID();
        jkPushTemplate.setId(id);
        jkPushTemplate.setIsDelete("0");
        jkPushTemplate.setCreateTime(DateUtils.getNowDate());

        if (jkPushTemplate.getTemplateList().size() > 0) {
            ArrayList<JkPushTemplateItem> addList = new ArrayList<>();

            Date nowDate = DateUtils.getNowDate();
            jkPushTemplate.getTemplateList().forEach(templateId -> {
                JkPushTemplateItem jkPushTemplateItem = new JkPushTemplateItem();
                jkPushTemplateItem.setCreateTime(nowDate);
                jkPushTemplateItem.setTemplateId(templateId);
                jkPushTemplateItem.setPushTemplateId(id);
                addList.add(jkPushTemplateItem);
            });
            this.saveBatch(addList);

        }


        return jkPushTemplateMapper.insertJkPushTemplate(jkPushTemplate);
    }

    /**
     * 修改推送模版
     *
     * @param jkPushTemplate 推送模版
     * @return 结果
     */
    @Override
    public int updateJkPushTemplate(JkPushTemplate jkPushTemplate) {

        jkPushTemplateMapper.delPushTemplate(jkPushTemplate.getId());
        if (jkPushTemplate.getTemplateList().size() > 0) {
            ArrayList<JkPushTemplateItem> addList = new ArrayList<>();

            Date nowDate = DateUtils.getNowDate();
            jkPushTemplate.getTemplateList().forEach(templateId -> {
                JkPushTemplateItem jkPushTemplateItem = new JkPushTemplateItem();
                jkPushTemplateItem.setCreateTime(nowDate);
                jkPushTemplateItem.setTemplateId(templateId);
                jkPushTemplateItem.setPushTemplateId(jkPushTemplate.getId());
                addList.add(jkPushTemplateItem);
            });
            this.saveBatch(addList);

        }

        return jkPushTemplateMapper.updateJkPushTemplate(jkPushTemplate);
    }

    /**
     * 批量删除推送模版
     *
     * @param ids 需要删除的推送模版主键
     * @return 结果
     */
    @Override
    public int deleteJkPushTemplateByIds(String[] ids) {
        return jkPushTemplateMapper.deleteJkPushTemplateByIds(ids);
    }

    /**
     * 删除推送模版信息
     *
     * @param id 推送模版主键
     * @return 结果
     */
    @Override
    public int deleteJkPushTemplateById(String id) {
        return jkPushTemplateMapper.deleteJkPushTemplateById(id);
    }


    @Override
    public PageData toAddMessagePush(PageData pageData) {

        PageData result = new PageData();
        JkPushTemplate jkPushTemplate = new JkPushTemplate();
        jkPushTemplate.setPushType(pageData.get("pushType") + "");
        List<JkPushTemplate> list = selectJkPushTemplateList(jkPushTemplate);

        if (list.size() != 1) {
            result.put("msg", "模板推送设置异常");
            result.put("code", 201);
            result.put("data", "");
            return result;
        }

        JkPushTemplate pushTrmp = list.get(0);

        PageData params = new PageData();
        params.put("id", UuidUtil.get32UUID());
        params.put("title", pushTrmp.getName());
        params.put("type", pushTrmp.getPushType());
        params.put("userId", pageData.get("userId"));//患者id
        params.put("pushTemplateId", pushTrmp.getId());
        params.put("createTime", DateUtils.getNowDate());
        params.put("name", "");
        //type:1入院推送，4出院推送
        jkPushTemplateMapper.insertJkInterfaceMessage(params);
        result.put("msg", "成功");
        result.put("code", 200);
        result.put("data", "");
        return result;

    }

    @Override
    public void sendMessageWx(int num) {
           /* PageData PARAMS= new PageData();
        PARAMS.put("pushType","入院推送");
        PARAMS.put("userId",33);*/

    /*
         <!--手术前一天-->
        <if test="operationTimeSubVal != null">

        <!--手术后一天-->
        <if test="operationTimeAddVal != null">


        <!--出院时间-->
        <if test="dischargeTimeVal != null">


        <!--院后14 天-->
        <if test="dischargeTimeAddTenVal != null">

        <!--院后30 天-->
        <if test="dischargeTimeAddThreeVal != null">*/


        PageData putGetVal = new PageData();


        PageData paramsPush = new PageData();
        List<PageData> list = jkMessageService.selectJkPushTemplateList(paramsPush);
        for (int i = 0; i < list.size(); i++) {
            PageData pd = list.get(i);
            String pushIllnessType = pd.get("pushIllnessType") + "";
            String name = pd.get("pushIllnessType") + "";
            String pushSelect = pd.get("pushSelect") + "";
            String pushType = pd.get("pushType") + "";
            String key = pushType + ":" + pushIllnessType + pushSelect;
            paramsPush.put(key, pd);
        }
        List<JkInterfaceMessage> allList = new ArrayList<>();


        if (num == 1) {


            //手术前一天---术前推送
            PageData a1params = new PageData();
            a1params.put("operationTimeSubVal", "1");
            List<PageData> a1 = jkMessageService.selectHospitalizationList(a1params);
            for (PageData pd : a1) {
                addParamsList(pd, "术前推送", "", allList, paramsPush);
            }


            //手术后一天-----术后推送
            PageData a2params = new PageData();
            a2params.put("operationTimeAddVal", "1");
            List<PageData> a2 = jkMessageService.selectHospitalizationList(a2params);
            for (PageData pd : a2) {
                addParamsList(pd, "术后推送", "", allList, paramsPush);
            }


            //院后14 天  ----院后推送:通用14
            PageData a4params = new PageData();
            a4params.put("dischargeTimeAddTenVal", "1");
            List<PageData> a4 = jkMessageService.selectHospitalizationList(a4params);

            for (PageData pd : a4) {
                addParamsList(pd, "院后推送:通用", "14", allList, paramsPush);
            }
            //院后30 天
            PageData a5params = new PageData();
            a5params.put("dischargeTimeAddThreeVal", "1");
            List<PageData> a5 = jkMessageService.selectHospitalizationList(a5params);
            for (PageData pd : a5) {
                addParamsList(pd, "院后推送:通用", "30", allList, paramsPush);
            }


        }

        if (num == 2) {
            //出院时间-----出院推送
            PageData a3params = new PageData();
            a3params.put("dischargeTimeVal", "1");
            List<PageData> a3 = jkMessageService.selectHospitalizationList(a3params);
            for (PageData pd : a3) {
                addParamsList(pd, "出院推送:通用", "", allList, paramsPush);
            }
        }

        if (allList.size() > 0) {

            iJkInterfaceMessageService.addInterfaceMessageList(allList);
            System.out.println(allList);
            iJkPushService.addWxAddMessage(allList);
        }


    }


    public void addParamsList(PageData pd, String key, String selectVal, List<JkInterfaceMessage> allList, PageData paramsPush) {
    /*        "院后推送:通用30" -> {PageData@14025}  size = 5
            "术前推送:上肢" -> {PageData@14027}  size = 5
            "术后推送:上肢" -> {PageData@14029}  size = 5
            "术前推送:下肢" -> {PageData@14031}  size = 5
            "术后推送:下肢" -> {PageData@14007}  size = 5
            "院后推送:通用14" -> {PageData@14033}  size = 5
            "入院推送:通用" -> {PageData@14035}  size = 5
            "出院推送:通用" -> {PageData@14037}  size = 5*/


        try {
            String disease = pd.getString("disease");//冰病种
            String openId = pd.getString("openId");//
            String unionId = pd.getString("unionId");//
            Long userId = Long.parseLong(pd.get("userId") + "");//
            String pushTemplateId = pd.get("id") + "";//
            String pkey = key + ":" + disease + selectVal;
            if (StringUtils.equals(key, "出院推送:通用") || StringUtils.equals(key, "院后推送:通用")) {
                pkey = key + selectVal;
            }

            PageData data = (PageData) paramsPush.get(pkey);
            JkInterfaceMessage jkInterfaceMessage = new JkInterfaceMessage();

            jkInterfaceMessage.setCreateTime(DateUtils.getNowDate());
            jkInterfaceMessage.setPushTemplateId(pushTemplateId);
            jkInterfaceMessage.setTitle(data.getString("name"));
            jkInterfaceMessage.setUserId(userId);
            jkInterfaceMessage.setType(data.getString("pushType"));
            jkInterfaceMessage.setOpenId(openId);
            jkInterfaceMessage.setUnionId(unionId);


            allList.add(jkInterfaceMessage);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }
}
